package com.zhc.tbschedule.demo.task;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.taobao.pamirs.schedule.IScheduleTaskDealSingle;
import com.taobao.pamirs.schedule.TaskItemDefine;

/**
 * 说明：测试批量处理任务接口
 * @author jinsicao
 * @version 2018年6月26日
 */
public class TestSingleTask implements IScheduleTaskDealSingle<String> {

	@Override
	public List<String> selectTasks(String taskParameter, String ownSign, int queueNum, List<TaskItemDefine> queryCondition, int fetchNum)
			throws Exception {
		if (queryCondition.size() == 0) {
			return Collections.emptyList();
		}
		if (queryCondition.size() != queueNum) {
			fetchNum = fetchNum * queueNum / queryCondition.size();
		}
		List<String> taskList = new ArrayList<String>();
		taskList.add("testOne");
		taskList.add("testTwo");
		taskList.add("testThree");
		taskList.add("testFour");
		taskList.add("testFive");
		return taskList;
	}

	@Override
	public boolean execute(String task, String ownSign) throws Exception {
		System.out.println(task);
		return true;
	}
	
	@Override
	public Comparator<String> getComparator() {
		return new Comparator<String>() {

			@Override
			public int compare(String o1, String o2) {
				if (StringUtils.isNotBlank(o1)) {
					if (o1.equals(o2)) {
						return 0;
					}
				}
				return 1;
			}
			
		};
	}
	
	private boolean isMyTask(long taskCount, long id, List<?> subQueues) {
		if (taskCount == subQueues.size()) {
			return true;
		}
		long m = id % taskCount;
		for (Object o : subQueues) {
			if (m == Long.parseLong(o.toString()))
				return true;
		}
		return false;
	}

}
